#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.basic.module.group.controller;

import ${package}.basic.module.group.service.GroupService;
import ${package}.common.bean.po.Group;
import ${package}.common.bean.po.Tree;
import ${package}.common.bean.vo.GroupVo;
import ${package}.common.bean.vo.JsonResult;
import ${package}.common.bean.vo.QueryBean;
import ${package}.common.emum.ReturnCodeEnum;
import ${package}.common.utils.SnowFlakeUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * Description: 组织
 *
 * @author YL
 * @date 2019/8/6 15:28
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Slf4j
@RestController
@RequestMapping("/group/")
@AllArgsConstructor
@Api(value = "组织")
@Validated
public class GroupController {

    /**
     * 组织Service
     */
    private final GroupService groupService;

    /**
     * Description:
     * <根据id查找机构>
     * @author 周婷婷
     * @date 16:53 2019/8/6
     * @param id 1
     * @return JsonResult
     **/
    @GetMapping("getGroupById")
    @ApiOperation(value = "根据id查找机构",notes = "根据id查找机构")
    @ApiImplicitParam(name = "id",value = "机构主键",dataType = "Long",paramType = "query",required = true)
    public JsonResult<Group> getGroupById(@RequestParam Long id){
        Group group = groupService.getGroupById(id);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS,group);
    }

    /**
     * Description:
     * <根据id查找该机构到根节点的所有机构>
     * @author 周婷婷
     * @date 16:54 2019/8/6
     * @param id 1
     * @return JsonResult
     **/
    @GetMapping("getGroupChain")
    @ApiOperation(value = "根据id查找该机构到根节点的所有机构",notes = "根据id查找该机构到根节点的所有机构")
    @ApiImplicitParam(name = "id",value = "机构id",dataType = "Long",paramType = "query",required = true)
    public JsonResult<List<Group>> getGroupChain(Long id){
        List<Group> list = groupService.getGroupChain(id);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS,list);
    }

    /**
     * Description:
     * <获取机构列表>
     * @author 周婷婷
     * @date 16:54 2019/8/6
     * @param queryBean 1
     * @return JsonResult
     **/
    @PostMapping("groupList")
    @ApiOperation(value = "获取机构列表",notes = "获取机构列表")
    @ApiImplicitParam(name = "queryBean",value = "查询信息",dataType = "QueryBean",paramType = "body",required = true)
    public JsonResult<PageInfo<Group>> queryGroupList(@RequestBody @Valid QueryBean queryBean){
        PageInfo<Group> pageInfo = groupService.queryGroupListByLimit(queryBean);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, pageInfo);
    }

    /**
     * Description:
     * <获取机构类型列表，用于前台下拉框查询>
     * @author 周婷婷
     * @date 16:54 2019/8/6
     * @return JsonResult
     **/
    @GetMapping("getGroupTypeList")
    @ApiOperation(value = "获取机构类型列表，用于前台下拉框查询",notes = "获取机构类型列表，用于前台下拉框查询")
    public JsonResult<List<Integer>> getGroupTypeList(){
        List<Integer> list = groupService.getGroupTypeList();
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS,list);
    }

    /**
     * Description:
     * <新增机构>
     * @author 周婷婷
     * @date 16:55 2019/8/6
     * @param groupVo 1
     * @return JsonResult
     **/
    @PutMapping("addGroup")
    @ApiOperation(value = "新增机构",notes = "新增机构")
    @ApiImplicitParam(name = "groupVo",value = "机构实体",dataType = "GroupVo",paramType = "body",required = true)
    public JsonResult<Group> addGroup(@RequestBody @Valid GroupVo groupVo){

        Group group = new Group();
        BeanUtils.copyProperties(groupVo,group);
        group.setId(SnowFlakeUtil.getID());

        groupService.addGroup(group);

        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, group);
    }

    /**
     * Description:
     * <查询机构编码是否存在>
     * @author 周婷婷
     * @date 16:55 2019/8/6
     * @param code 1
     * @param id 2
     * @return JsonResult
     **/
    @GetMapping("groupVerifyCode")
    @ApiOperation(value = "查询机构编码是否存在",notes = "查询机构编码是否存在")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code",value = "机构编码",dataType = "String",paramType = "query",required = true),
            @ApiImplicitParam(name = "id",value = "机构主键",dataType = "Long",paramType = "query",required = true)})
    public JsonResult<Boolean> groupVerifyCode(@RequestParam("code") String code,@RequestParam("id") Long id){
        Boolean flag = groupService.groupVerifyCode(code,id);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS,flag);
    }

    /**
     * Description:
     * <根据id更新机构信息>
     * @author 周婷婷
     * @date 16:56 2019/8/6
     * @param groupVo 1
     * @return JsonResult
     **/
    @PostMapping("updateGroupById")
    @ApiOperation(value = "根据id更新机构信息",notes = "根据id更新机构信息")
    @ApiImplicitParam(name = "groupVo",value = "组织实体类",dataType = "GroupVo",paramType = "body",required = true)
    public JsonResult<Group> updateGroupById(@RequestBody @Valid GroupVo groupVo){
        Group group = new Group();
        BeanUtils.copyProperties(groupVo,group);

        groupService.updateGroupById(group);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, group);
    }

    /**
     * Description:
     * <根据id删除机构>
     * @author 周婷婷
     * @date 16:56 2019/8/6
     * @param id 1
     * @return JsonResult
     **/
    @DeleteMapping("deleteGroup/{id}")
    @ApiOperation(value = "根据id删除机构",notes = "根据id删除机构")
    @ApiImplicitParam(name = "id",value = "机构主键",dataType = "Long",paramType = "path",required = true)
    public JsonResult<Long> deleteGroup(@PathVariable("id") Long id){

        groupService.deleteGroup(id);

        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, id);
    }

    /**
     * Description:
     * <获取组织树>
     * @author songcx
     * @date 10:44 2019/9/4
     * @return ${package}.common.bean.vo.JsonResult<java.util.List<${package}.common.bean.po.Tree>>
     **/
    @GetMapping("groupTree")
    @ApiOperation(value = "获取组织树",notes = "获取组织树")
    public JsonResult<List<Tree>> groupTree(){
        List<Tree> treeList =  groupService.getGroupTree();
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS,treeList);
    }

}
